package com.sunday.android.fangdaifu.bukong.activity;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.fragment.HouseDetailFragment;
import com.sunday.android.fangdaifu.bukong.fragment.tab.NewsFragment;
import com.sunday.common.adapter.MainFragmentPagerAdapter;
import com.sunday.member.base.BaseActivity;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by 刘涛 on 2017/11/6.
 */

public class HouseDetailActivity extends BaseActivity {


    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.left_txt)
    TextView leftTxt;
    @Bind(R.id.tab_layout)
    TabLayout tabLayout;
    @Bind(R.id.select_status)
    TextView selectStatus;
    @Bind(R.id.view_pager)
    ViewPager viewPager;


    private int houseId;
    private String houseName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_detail);
        ButterKnife.bind(this);
        initView();
        initFragments();
    }

    private void initView() {
        houseId=getIntent().getIntExtra("houseId",0);
        houseName=getIntent().getStringExtra("houseName");
        titleView.setText(houseName);
    }

    private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    private void initFragments(){
        fragments.add(HouseDetailFragment.newInstance(0,houseId));
        fragments.add(HouseDetailFragment.newInstance(1,houseId));
        fragments.add(HouseDetailFragment.newInstance(2,houseId));
        fragments.add(HouseDetailFragment.newInstance(3,houseId));
        viewPager.setAdapter(new MainFragmentPagerAdapter(getSupportFragmentManager(),mContext,fragments,R.array.house_detail_type));
        viewPager.setOffscreenPageLimit(fragments.size());
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setCurrentItem(0);
    }


    //排序搜索
    @OnClick(R.id.select_status)
    void sort() {
        backgroundAlpha(0.7f);
        showPopupwindow();
    }


    PopupWindow popupWindow;

    //显示PopupWindow
    private void showPopupwindow() {
        if (popupWindow != null) {
            popupWindow.dismiss();

        }
        popupWindow = new PopupWindow(mContext);
        View view = getWindow().getLayoutInflater().inflate(R.layout.layout_sort, null);
        popupWindow.setContentView(view);
        popupWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setOnDismissListener(new PoponDismissListener());

        // 使其聚集
        popupWindow.setFocusable(true);
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAsDropDown(tabLayout);
//        popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
        view.findViewById(R.id.sort_warning).setOnClickListener(onClickListener);
        view.findViewById(R.id.sort_normal).setOnClickListener(onClickListener);

    }

    private int status;//1-预警0-正常
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popupWindow.dismiss();
            TextView view = (TextView) v;
            switch (v.getId()) {
                case R.id.sort_warning:
                    selectStatus.setText(view.getText());
                    status = 1;
//                    ((NewsFragment)fragments.get(tabLayout.getSelectedTabPosition())).updateView(ordeBy);
                    break;
                case R.id.sort_normal:
                    status = 0;
                    selectStatus.setText(view.getText());
//                    ((NewsFragment)fragments.get(tabLayout.getSelectedTabPosition())).updateView(ordeBy);
                    break;
            }
            for (int i = 0; i < tabLayout.getTabCount(); i++) {
                ((HouseDetailFragment) fragments.get(i)).updateView(status);
            }
//            ((NewsFragment)fragments.get(tabLayout.getSelectedTabPosition())).updateView(ordeBy);
        }
    };


    /**
     * 弹出的popWin关闭的事件，主要是为了将背景透明度改回来
     *
     * @author cg
     */
    class PoponDismissListener implements PopupWindow.OnDismissListener {

        @Override
        public void onDismiss() {
            // TODO Auto-generated method stub
            backgroundAlpha(1f);
        }
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }

}
